package com.example.administrator.footballgamecollectandanalyze;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestHandle;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

    private Button btnRegister1;
    TextView tvUserName;
    TextView tvPassword;
    TextView tvPassword1;
    TextView phone;

    String url = "http://112.74.26.238:8080/football/register";
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        tvUserName = (TextView) findViewById(R.id.userName);
        tvPassword = (TextView) findViewById(R.id.password);
        tvPassword1 = (TextView) findViewById(R.id.password1);
        btnRegister1 = (Button) findViewById(R.id.Register_button1);
        phone = (TextView) findViewById(R.id.phone);

        btnRegister1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String userName = tvUserName.getText().toString();// 用户名
                String userPass = tvPassword.getText().toString();// 用户密码
                String userPass1 = tvPassword1.getText().toString();// 确认密码
                String phone1 = phone.getText().toString();// 电话号码
                //判断用户名和密码是否为空
                if (TextUtils.isEmpty(userName.trim())
                        || TextUtils.isEmpty(userPass.trim())
                        || TextUtils.isEmpty(userPass1.trim())
                        || TextUtils.isEmpty(phone1.trim())) {
                    Toast.makeText(RegisterActivity.this, "所有选项均不能为空", Toast.LENGTH_SHORT).show();
                }
                else if(!userName.matches("\\d{18}"))
                {
                    Toast.makeText(RegisterActivity.this, "请输入有效的18位身份证号码", Toast.LENGTH_SHORT).show();
                }
                else if(!userPass.equals(userPass1))
                {
                    Toast.makeText(RegisterActivity.this, "两次输入密码不同", Toast.LENGTH_SHORT).show();
                }
                else if(!phone1.matches("\\d{11}"))
                {
                    Toast.makeText(RegisterActivity.this, "请输入有效的11位电话号码", Toast.LENGTH_SHORT).show();
                }
                else {
                    // 发送请求给服务器
                    //调用：loginByAsyncHttpClientPost(userName, userPass);
                    loginByAsyncHttpClientPost(userName, userPass,phone1);
                }
            }
        });

    }

    public void loginByAsyncHttpClientPost(String userName, String userPass,String phone1) {
        AsyncHttpClient client = new AsyncHttpClient(); // 创建异步请求的客户端对象
        // 创建请求参数的封装的对象
        RequestParams params = new RequestParams();
        params.put("username", userName); // 设置请求的参数名和参数值
        params.put("password", userPass);
        params.put("phone", phone1);
        // 执行post方法
        RequestHandle post = client.get(url, params, new AsyncHttpResponseHandler() {
            /**
             * 成功处理的方法
             * responseBody:响应内容的字节
             */
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  byte[] responseBody) {
                if (statusCode == 200) {
                    String str = new String(responseBody);
                    try {
                        JSONObject jsonObject = new JSONObject(str);
                        String code = jsonObject.getString("code");
                        if (code.equals("0")) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                        } else {
                            Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }

            /**
             * 失败处理的方法
             * error：响应失败的错误信息封装到这个异常对象中
             */
            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  byte[] responseBody, Throwable error) {
                error.printStackTrace();// 把错误信息打印出轨迹来
                //Toast.makeText(MyApplication.getContext(), "网络错误", Toast.LENGTH_LONG).show();
            }
        });
    }

}
